package com.book.auth.handler;

import com.alibaba.fastjson.JSON;
import com.book.core.enums.REnum;
import com.book.core.mode.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**用户无权限访问的
 * @author hongmizfb
 * @date 2025/05/09
 */
@Component
@Slf4j
public class CustomAccessDeniedHandler implements AccessDeniedHandler {

    @Override
    public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException accessDeniedException) throws IOException, ServletException {
        response.setContentType("application/json;charset=UTF-8");
        log.error("用户无权限访问!", accessDeniedException);
        response.getWriter().write(JSON.toJSONString(R.err(REnum.UNAUTHORIZED)));
    }
}
